//
//  StatusOriginalView.swift
//  Weibo
//
//  Created by 明丽 on 16/1/18.
//  Copyright © 2016年 明丽. All rights reserved.
//

import UIKit
import SnapKit

class StatusOriginalView: UIView {
    //定义约束对象属性 来进行记录  Constraint 就是SnapKit中约束对象的类型
    var bottomConstant: Constraint?

    //绑定数据
    var status: Status? {
        didSet{
            headImage.sd_setImageWithURL(status?.user?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLable.text = status?.user?.name
            mbrankImage.image = status?.user?.mbrank_image
            verifiedTypeImage.image = status?.user?.verified_type_image
            titleLable.text = status?.crested_at
            sourceLable.text = status?.source
            contentLablel.text = status?.text
            //配视图
            prctureView.imageURLs = status?.imageURLs
            //根据配视图动态调整视图约束
            self.bottomConstant?.uninstall()
            if let urls = status?.imageURLs{
                print(urls.count)

            //表示不为空
                self.snp_updateConstraints (closure: { (make) -> Void in
                    self.bottomConstant =   make.bottom.equalTo(prctureView.snp_bottom).offset(StatusCellMargin).constraint
                })

            }else{
            //表示为空
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstant = make.bottom.equalTo( contentLablel.snp_bottom).offset(StatusCellMargin).constraint
                })
                

            
            }
            
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置ui界面
    private func setupUI() {
       // backgroundColor = UIColor(white: 0.95, alpha: 1)
        //设置分割线
        sepView.backgroundColor = UIColor.redColor()
        addSubview(sepView)
        //添加到视图上
        addSubview(headImage)
        addSubview(nameLable)
        addSubview(mbrankImage)
        addSubview(titleLable)
        addSubview(sourceLable)
        addSubview(verifiedTypeImage)
        addSubview(contentLablel)
        addSubview(prctureView)
        //设置分割线约束
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        
        //设置约束
        headImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: StatusCellImageWidth, height: StatusCellImageWidth))
            
        }
        
        nameLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImage.snp_top)
            make.left.equalTo(headImage.snp_right).offset(StatusCellMargin)
        }
        
        mbrankImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLable.snp_top)
            make.left.equalTo(nameLable.snp_right).offset(StatusCellMargin)
        }
        
        titleLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImage.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(headImage.snp_bottom)
            
        }
        
        sourceLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(titleLable.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(titleLable.snp_bottom)
        }
        
        verifiedTypeImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImage.snp_right)
            make.centerY.equalTo(headImage.snp_bottom)
        }
        
        contentLablel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImage.snp_left)
            //            make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
            make.top.equalTo(headImage.snp_bottom).offset(StatusCellMargin)
        }
        //设置配视图的约束
        prctureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImage.snp_left)
            make.top.equalTo(contentLablel.snp_bottom).offset(StatusCellMargin)
            //以后修改,先给个测试大小
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        
        
        //视图的行高自动获取
        self.snp_makeConstraints { (make) -> Void in
         self.bottomConstant =   make.bottom.equalTo(prctureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
        
        
    }
    //分割线
    private lazy var sepView: UIView = UIView()
    //懒加在图片
    private lazy var headImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //名字
    private lazy var nameLable: UILabel = UILabel(title: "哈哈哈哈哈", fontSize: 14, color: themeColor)
    //用户等级
    private lazy var mbrankImage: UIImageView =  UIImageView(image: UIImage(named: "common_icon_membership"))
    //微博时间
    private lazy var titleLable: UILabel = UILabel(title: "10:17", fontSize: 10, color: UIColor.lightGrayColor())
    //微博来源
    private lazy var sourceLable:UILabel = UILabel(title: "微博来源", fontSize: 10, color: UIColor.lightGrayColor())
    //微博认证类型
    private lazy var verifiedTypeImage: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //微博正文
    private lazy var contentLablel: UILabel = UILabel(title: "听说我们不曾落泪", fontSize: 14, color: UIColor.darkGrayColor(),margin: StatusCellMargin)
    
    //添加配视图
    private lazy var prctureView: StayusCellPictureView = StayusCellPictureView()

    
    
    
}
